package com.xo.xodemo.util;

import com.github.pagehelper.util.StringUtil;
import org.apache.commons.io.FilenameUtils;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

public class MoneyFormatterUtil {
    private static final String[] CN_UPPER_NUMBER = {"零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"};
    private static final String[] CN_UPPER_UNIT = {"佰", "拾","万","仟", "佰", "拾", "元", "角", "分"};

    public static String formatAmount(double amount) {
        String result="";
        BigDecimal number = new BigDecimal(amount);
        int nonezeroindex=0;
        int millionthDigit =  number.divide(new BigDecimal("1000000")).remainder(BigDecimal.TEN).intValue();
        int oneHundredThousandDigit =  number.divide(new BigDecimal("100000")).remainder(BigDecimal.TEN).intValue();
        int TenThousandDigit =  number.divide(new BigDecimal("10000")).remainder(BigDecimal.TEN).intValue();
        int ThousandDigit =  number.divide(new BigDecimal("1000")).remainder(BigDecimal.TEN).intValue();
        int HundredDigit =  number.divide(new BigDecimal("100")).remainder(BigDecimal.TEN).intValue();
        int TenDigit =  number.divide(new BigDecimal("10")).remainder(BigDecimal.TEN).intValue();
        int Digit =  number.divide(new BigDecimal("1")).remainder(BigDecimal.TEN).intValue();
        int decimalFirstDigit = 0;
        DecimalFormat decimalFormat = new DecimalFormat("#.00");
        String numberStr = decimalFormat.format(number);
        int decimalSecondDigit = 0;
        int decimalIndex = numberStr.indexOf('.');
        String decimalPart1 = numberStr.substring(decimalIndex + 1, decimalIndex + 2);
        decimalFirstDigit = Integer.parseInt(decimalPart1);
        String decimalPart2 = numberStr.substring(decimalIndex + 2, decimalIndex + 3);
        decimalSecondDigit = Integer.parseInt(decimalPart2);
        if(millionthDigit!=0||nonezeroindex==1){
            nonezeroindex=1;
            result+=CN_UPPER_NUMBER[millionthDigit]+","+CN_UPPER_UNIT[0]+",";
        }
        if(oneHundredThousandDigit!=0||nonezeroindex==1){
            nonezeroindex=1;
            result+=CN_UPPER_NUMBER[oneHundredThousandDigit]+","+CN_UPPER_UNIT[1]+",";
        }
        if(TenThousandDigit!=0||nonezeroindex==1){
            nonezeroindex=1;
            result+=CN_UPPER_NUMBER[TenThousandDigit]+","+CN_UPPER_UNIT[2]+",";
        }
        if(ThousandDigit!=0||nonezeroindex==1){
            nonezeroindex=1;
            result+=CN_UPPER_NUMBER[ThousandDigit]+","+CN_UPPER_UNIT[3]+",";
        }
        if(HundredDigit!=0||nonezeroindex==1){
            nonezeroindex=1;
            result+=CN_UPPER_NUMBER[HundredDigit]+","+CN_UPPER_UNIT[4]+",";
        }
        if(TenDigit!=0||nonezeroindex==1){
            nonezeroindex=1;
            result+=CN_UPPER_NUMBER[TenDigit]+","+CN_UPPER_UNIT[5]+",";
        }
        if(Digit!=0||nonezeroindex==1){
            nonezeroindex=1;
            result+=CN_UPPER_NUMBER[Digit]+","+CN_UPPER_UNIT[6]+",";
        }
        if(decimalFirstDigit!=0||nonezeroindex==1){
            nonezeroindex=1;
            result+=CN_UPPER_NUMBER[decimalFirstDigit]+","+CN_UPPER_UNIT[7]+",";
        }
        if(decimalSecondDigit!=0||nonezeroindex==1){
            nonezeroindex=1;
            result+=CN_UPPER_NUMBER[decimalSecondDigit]+","+CN_UPPER_UNIT[8];
        }

        return result.toString();
    }
}